package com.ece.pfe_rover_pvt_ines_ece_application.view;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.ece.pfe_rover_pvt_ines_ece_application.PFE_ROVER_PVT_INES_ECE_Application;
import com.ece.pfe_rover_pvt_ines_ece_application.R;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.ui.ParseLoginBuilder;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;


public class GraphsActivity extends Activity{
	public boolean DEBUG_MODE = PFE_ROVER_PVT_INES_ECE_Application.DEBUG;
	private static final int LOGIN_ACTIVITY_CODE = 100;
	
	private ActionBar actionBar;
	private Calendar startDate;
	private long dateFilter=0l;
	
	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
	
	@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_graphs);
	      //Menu & subtitle		
	        actionBar= getActionBar();
			actionBar.setSubtitle(R.string.subtitle_graphs);
			
			int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");		
			((RadioButton) findViewById(R.id.rb_default)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_last12h)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_last24h)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_last48h)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_last72h)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_lastHour)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_lastw)).setButtonDrawable(id);
			((RadioButton) findViewById(R.id.rb_default)).setButtonDrawable(id);
			
			startDate = new GregorianCalendar();			
			
	    }
	    
	    public void IntensiteGraphHandler (View view)
	    {	    	
	    	LineGraph line = new LineGraph();
	    	Intent lineIntent = line.getIntent(this, dateFilter);		    		    	
	        startActivity(lineIntent);
	    }
	    
	    public void Intensite2GraphHandler (View view)
	    {
	    	LineGraph2 line = new LineGraph2();
	    	Intent lineIntent = line.getIntent(this, dateFilter);	    	
	        startActivity(lineIntent);
	    	
	    }
	    
	    public void Intensite3GraphHandler (View view)
	    {
	    	LineGraph3 line = new LineGraph3();
	    	Intent lineIntent = line.getIntent(this, dateFilter);
	        startActivity(lineIntent);
	    }
	    
	    public void PuissanceGraphHandler (View view)
	    {
	    	PuissanceGraph line = new PuissanceGraph();
	    	Intent lineIntent = line.getIntent(this, dateFilter);
	        startActivity(lineIntent);
	    }
	    
	    public void LumGraphHandler (View view)
	    {
	    	LumGraph line = new LumGraph();
	    	Intent lineIntent = line.getIntent(this, dateFilter);
	        startActivity(lineIntent);
	    }
	    
	    public void TempGraphHandler (View view)
	    {
	    	TempPVGraph line = new TempPVGraph();
	    	Intent lineIntent = line.getIntent(this, dateFilter);
	        startActivity(lineIntent);
	    }
	    
	    /**
		 * OnCreateOptionsMenu (Android method related to Activity class)
		 * Inflate the menu
		 */
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {		
			// Inflate the menu
			//this adds items to the action bar if it is present.		
			getMenuInflater().inflate(R.menu.action_logout, menu);
			getMenuInflater().inflate(R.menu.activity_main_actions, menu);		
			getMenuInflater().inflate(R.menu.action_login, menu);							
			
			return true;
		}

		/**
		 * onOptionsItemSelected (Android method related to Activity class)
		 * Start the activity related to a selected field in the menu
		 */
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {		
			// Handle action bar item clicks here. The action bar will
			// automatically handle clicks on the Home/Up button, so long
			// as you specify a parent activity in AndroidManifest.xml.
			int id = item.getItemId();
			switch(id) {
			
			case R.id.measurements_list:
				startActivity(new Intent(this.getBaseContext(),MeasureActivity.class));			
				return true;
			case R.id.measurements_table:
				startActivity(new Intent(this.getBaseContext(),TableActivity.class));			
				return true;
				
			case R.id.referentiel:
				startActivity(new Intent(this.getBaseContext(),ReferentielActivity.class));			
				return true;
			
				
			case R.id.action_logout:			
				// Log out the current user
				ParseUser.logOut();
				// Create a new anonymous user
				ParseAnonymousUtils.logIn(null);
							
				// Unpin all the current objects
				ParseObject
						.unpinAllInBackground(PFE_ROVER_PVT_INES_ECE_Application.MEASUREMENTS_GROUP_NAME);
				
				//End the activity & go back the the previous one
				finish();
				
				return super.onOptionsItemSelected(item);
				
			case R.id.action_login:
				invalidateOptionsMenu();
				ParseLoginBuilder builder = new ParseLoginBuilder(this);
				startActivityForResult(builder.build(), LOGIN_ACTIVITY_CODE);
				
				return super.onOptionsItemSelected(item);
			
			default:			
					return super.onOptionsItemSelected(item);
			}
		}
		
		/**
		 * onPrepareOptionsMenu
		 */
		@Override
		public boolean onPrepareOptionsMenu(Menu menu) {		
			
			if(DEBUG_MODE) System.out.println("---- Entering onPrepareOptopnsMenu method...");
			
			boolean realUser;
			super.onPrepareOptionsMenu(menu);
			if(ParseUser.getCurrentUser() != null){
				realUser = !ParseAnonymousUtils.isLinked(ParseUser
					.getCurrentUser());
			
				menu.findItem(R.id.action_login).setVisible(!realUser);						
				
				//Only registered user can see these buttons
				menu.findItem(R.id.action_logout).setVisible(realUser);	
				menu.findItem(R.id.measurements_list).setVisible(realUser);
				menu.findItem(R.id.measurements_table).setVisible(realUser);
				menu.findItem(R.id.referentiel).setVisible(realUser);
			}	
			menu.findItem(R.id.measurements_graphs).setVisible(false);
			return true;
		}
		
		public void onRadioButtonClicked(View view) {
			System.out.println("haha "+sdf.format(dateFilter));
		    // Is the button now checked?
		    boolean checked = ((RadioButton) view).isChecked();
		    
		    // Check which radio button was clicked
		    switch(view.getId()) {
		        case R.id.rb_default:
		        	if (checked) {
		        		startDate.set(2015, 1, 1);
		        		System.out.println("1: "+startDate);
		        	}
		            break;
		        case R.id.rb_lastHour:
		        	if (checked) {
		        		startDate = Calendar.getInstance();
		        		startDate.add(Calendar.HOUR_OF_DAY, -1);
		        		dateFilter = startDate.getTimeInMillis();
		        		System.out.println("case rbLH " + dateFilter);
		        	}
		        	break;
		        case R.id.rb_last12h:
		            if (checked) {
		            	startDate = Calendar.getInstance();
		        		startDate.add(Calendar.HOUR_OF_DAY, -12);
		        		dateFilter = startDate.getTimeInMillis();
		            }
		            break;
		        case R.id.rb_last24h:
		        	if (checked) {
		        		startDate = Calendar.getInstance();
		        		startDate.add(Calendar.DAY_OF_MONTH, -1);
		        		dateFilter = startDate.getTimeInMillis();
		        	}
		            break;
		        case R.id.rb_last48h:
		        	if (checked) {
		        		startDate = Calendar.getInstance();
		        		startDate.add(Calendar.DAY_OF_MONTH, -2);
		        		dateFilter = startDate.getTimeInMillis();
		        	}
		            break;
		        case R.id.rb_last72h:
		        	if (checked) {		        		
		        		startDate = Calendar.getInstance();
		        		startDate.add(Calendar.DAY_OF_MONTH, -3);
		        		dateFilter = startDate.getTimeInMillis();
		        	}
		            break;
		        case R.id.rb_lastw:
		        	if (checked) {
		        		startDate = Calendar.getInstance();
		        		startDate.add(Calendar.DAY_OF_MONTH, -7);
		        		dateFilter = startDate.getTimeInMillis();
		        	}
		            break;
		    }
		}
}
